// pages/member/IntegralGeneralization/IntegralGeneralization.js
var app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
	 * 页面的初始数据
	 */
  data: {
    activityName: '',
    average: '',
    personNumber: '',
    credit1: 0,
    integral: '',
    clicktag: 0
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this._getIntegral();
    try {
      const value = wx.getStorageSync('integral');
      if (value) {
        this.setData({
          integral: value
        });
        // Do something with return value
        if (this.data.integral) {
          wx.setNavigationBarTitle({
            title: '发起' + this.data.integral + '赠送'
          });
        }
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  //活动名称值绑定
  activityNameinp(e) {
    let val = e.detail.value;
    this.setData({
      activityName: val
    });
  },
  //每人赠送值绑定
  averageinp(e) {
    let val = e.detail.value;
    this.setData({
      average: val
    });
  },
  //赠送数量值绑定
  personNumberinp(e) {
    let val = e.detail.value;
    this.setData({
      personNumber: val
    });
  },
  _getIntegral() {
    let that = this;
    let urlStr = app.getNetAddresss("finance.point-page.index");
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            credit1: res.data.credit1
          });
          wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: that.data.themeBaseColor,
            animation: {
              duration: 400,
              timingFunc: 'easeIn'
            }
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res.msg);
      }
    });
  },
  createActivity() {
    if (!this.data.activityName || !this.data.average || !this.data.personNumber) {
      wx.showToast({
        icon: 'none',
        title: '信息不能为空',
        duration: 1500
      });
      return;
    }
    if (!this.checkScore()) {
      return;
    }
    let that = this;
    if (this.data.clicktag === 0) {
      that.setData({
        clicktag: 1
      });
      let urlStr = app.getNetAddresss("plugin.point-activity.Frontend.Modules.Activity.Controllers.create.index");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          name: that.data.activityName,
          average: that.data.average,
          total: that.data.personNumber
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              activityName: '',
              average: '',
              personNumber: ''
            });
            wx.showToast({
              icon: 'none',
              title: '创建成功',
              duration: 1500
            });
            setTimeout(() => {
              that.setData({
                clicktag: 0
              });
              wx.navigateBack({
                delta: 1
              });
            }, 1000);
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }

        },
        fail: function(res) {}
      });
    }
  },
  checkScore() {
    let that = this;
    if (Number(this.data.average) * Number(this.data.personNumber) > Number(this.data.credit1)) {
      wx.showToast({
        icon: 'none',
        title: '您的' + (that.data.integral ? that.data.integral : '积分') + '不够',
        duration: 1500
      });
      return false;
    }
    return true;
  },
});
